package entity

import (
	"errors"
	"fmt"
	"time"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//经济资本
type CompTyywDrDtip struct {
	ID    int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	DTID  int       `gorm:"column:DTID"`
	JYDS  string    `gorm:"column:JYDS"`
	YWLX  string    `gorm:"column:YWLX"`
	JYJE  float64   `gorm:"column:JYJE"`
	QXR   time.Time `gorm:"column:QXR;type:date"`
	DQR   time.Time `gorm:"column:DQR;type:date"`
	TS    float64   `gorm:"column:TS"`
	LL    float64   `gorm:"column:LL"`
	FTPLL float64   `gorm:"column:FTPLL"`
	LC    float64   `gorm:"column:LC"`
	TS2   float64   `gorm:"column:TS2"`
	MNLR  float64   `gorm:"column:MNLR"`
	NRJ   float64   `gorm:"column:NRJ"`
	KHJL1 string    `gorm:"column:KHJL1"`
	ZB1   float64   `gorm:"column:ZB1"`
	KHJL2 string    `gorm:"column:KHJL2"`
	ZB2   float64   `gorm:"column:ZB2"`
	KHJL3 string    `gorm:"column:KHJL3"`
	ZB3   float64   `gorm:"column:ZB3"`
	KHJL4 string    `gorm:"column:KHJL4"`
	ZB4   float64   `gorm:"column:ZB4"`
	KHJL5 string    `gorm:"column:KHJL5"`
	ZB5   float64   `gorm:"column:ZB5"`
	DRJG  string    `gorm:"column:DRJG"`

	EMID1 int `gorm:"column:EMID1"`
	EMID2 int `gorm:"column:EMID2"`
	EMID3 int `gorm:"column:EMID3"`
	EMID4 int `gorm:"column:EMID4"`
	EMID5 int `gorm:"column:EMID5"`
}

//TableName 经济资本
func (CompTyywDrDtip) TableName() string {
	return "T_COMP_TYYW_DR_DTIP"
}

//BeforeSave 保存前的触发器
func (i *CompTyywDrDtip) BeforeSave(tx *gorm.DB) (err error) {
	if i.KHJL1 != "" {
		sum := i.ZB1 + i.ZB2 + i.ZB3 + i.ZB4 + i.ZB5
		if sum != 100 {
			return errors.New("占比之和必须为100！")
		}
		var emid1, emid2, emid3, emid4, emid5 int
		var emnm1, emnm2, emnm3, emnm4, emnm5 string
		sqlEx := "select ID,SysUserName from T_SysUser where SysUserID=? or SysUserName=?"
		row := tx.Raw(sqlEx, i.KHJL1, i.KHJL1).Row()
		row.Scan(&emid1, &emnm1)
		if emid1 == 0 && emnm1 != "分行公共" {
			return fmt.Errorf("员工号1:%s在系统中没有找到！", i.KHJL1)
		}
		i.EMID1 = emid1
		if i.ZB2 > 0 {
			row = tx.Raw(sqlEx, i.KHJL2, i.KHJL2).Row()
			row.Scan(&emid2, &emnm2)
			if emid2 == 0 && emnm2 != "分行公共" {
				return fmt.Errorf("员工号2:%s在系统中没有找到！", i.KHJL2)
			}
			i.EMID2 = emid2
		}
		if i.ZB3 > 0 {
			row = tx.Raw(sqlEx, i.KHJL3, i.KHJL3).Row()
			row.Scan(&emid3, &emnm3)
			if emid3 == 0 && emnm3 != "分行公共" {
				return fmt.Errorf("员工号3:%s在系统中没有找到！", i.KHJL2)
			}
			i.EMID3 = emid3
		}
		if i.ZB4 > 0 {
			row = tx.Raw(sqlEx, i.KHJL4, i.KHJL4).Row()
			row.Scan(&emid4, &emnm4)
			if emid4 == 0 && emnm4 != "分行公共" {
				return fmt.Errorf("员工号4:%s在系统中没有找到！", i.KHJL4)
			}
			i.EMID4 = emid4
		}
		if i.ZB5 > 0 {
			row = tx.Raw(sqlEx, i.KHJL5, i.KHJL5).Row()
			row.Scan(&emid5, &emnm5)
			if emid5 == 0 && emnm5 != "分行公共" {
				return fmt.Errorf("员工号5:%s在系统中没有找到！", i.KHJL5)
			}
			i.EMID5 = emid5
		}
	}
	return
}

//导入后执行分户处理
func TyywDrAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	prams := make([]interface{}, 1)
	prams[0] = params["DTID"]
	tx.Exec("EXEC UP_COMP_TYYW_DR_DTIP ?", prams...)
	return nil
}

func CompTyywDrDtipEF() base.EntityFunc {
	return func() interface{} {
		entity := CompTyywDrDtip{}
		return &entity
	}
}
func CompTyywDrDtipESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CompTyywDrDtip
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("CompTyywDrDtip", base.NewDefaultDao(CompTyywDrDtipEF(), CompTyywDrDtipESF(), nil))
	base.SetAfterCallBack("CompTyywDrDtip", TyywDrAfterImport)
}
